package com.xuyh.mqtt.realtime.dal;


import com.xuyh.mqtt.realtime.dto.DeviceDataDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DeviceDataDalImpl implements DeviceDataDal {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<DeviceDataDto> findAll() {
        return mongoTemplate.findAll(DeviceDataDto.class);
    }

    @Override
    public DeviceDataDto save(DeviceDataDto deviceDataDto) {
        return mongoTemplate.save(deviceDataDto);
    }

    @Override
    public List<DeviceDataDto> findByCondition(List<String> addrs, Date beginTime, Date endTime) {
        Query query = new Query(Criteria.where("content.Addr").in(addrs).and("createTime").gte(beginTime).lte(endTime)).with(Sort.by(Sort.Order.desc("createTime")));
        return mongoTemplate.find(query, DeviceDataDto.class);
    }
}
